<?php
namespace Frieza\Modules\FilterObj\Controllers;

use Doraemon\cache\Queue;
use Illuminate\Support\MessageBag;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use App\Admin\Extensions\Tools\UserButton;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\TopicActivity;
use Doraemon\model\FilterObj;


class FilterObjController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    private function _filterWhere($params){
        if(isset($params['_columns_'])){
            unset($params['_columns_']);
        }
        if(isset($params['_pjax'])){
            unset($params['_pjax']);
        }
        $page = isset($params['page']) ? $params['page'] : 1;
        $pageSize = isset($params['per_page']) ? $params['per_page'] : self::PAGE_SIZE;
        $limit = [($page - 1) * $pageSize, $pageSize];
        unset($params['page']);
        $where = [];
        foreach ($params as $_k => &$_v) {
            if ($_v <> '') {
                $where[$_k] = $_v;
            }
        }
        $data = [
            'LIMIT' => $limit,
        ];

        if (!empty($where)) {
            $data['AND'] = $where;
        }

        return $data;
    }

    public function index(Request $request, Content $content)
    {
        $request = $request->all();
        $request = self::_filterWhere($request);
        return $content
            ->header('过滤内容管理')
            ->breadcrumb(
                ['text' => '探店过滤内容列表', 'url' => '/filterobj/index']
            )
            ->body(self::listAction($request));

    }

    public function listAction($request = [])
    {
        $filter_infos = FilterObj::getAll();
        $gridModel = new pGridDataModel($filter_infos, count($filter_infos), []);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disablePagination();
            $grid->disableFilter();
            $grid->disableRowSelector();

            $grid->column('id');
            $grid->column('obj_id', '内容id');
            $grid->column('edittime', '最后操作时间');
        });

        $list = $listGrid->render();

        return $list;
    }

    //删除
    public function del($id)
    {
        $data = FilterObj::getById($id);
        if($data) {
            $data['filter_status'] = FilterObj::STATUS_DELETE;
            $data['edittime'] = time();

            $rs = FilterObj::updateById($id, $data);
            if($rs) {
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);
            } else {
                return response()->json([
                    'status' => false,
                    'message' => "操作失败",
                ]);
            }
        } else {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
    }

    public function add(Request $request, Content $content)
    {

        $request = $request->all();
        return $content
            ->header('过滤内容管理')
            ->breadcrumb(
                ['text' => '探店过滤内容', 'url' => '/filterobj/index'],
                ['text' => '添加tab', 'url' => '/filterobj/index/create']
            )
            ->body(self::editAction($request, $id = 0));

    }

    public function edit(Request $request, Content $content, $id)
    {

        $request = $request->all();
        return $content
            ->header('过滤内容管理')
            ->breadcrumb(
                ['text' => '探店过滤内容', 'url' => '/filterobj/index'],
                ['text' => '修改tab', 'url' => '/filterobj/index/'.$id.'/edit']
            )
            ->body(self::editAction($request, $id));


    }

    public function editAction($request, $id = 0)
    {
        $data = [];
        if($id != 0) {
            //修改
            $data = FilterObj::getById($id);
            $form = new Form($data);
            $form->hidden('id');
        } else {
            //添加
            $form = new Form($data);
        }
        $form->hidden('id');
        $form->text('obj_id', '内容id')->setWidth(4, 2)->required('不能为空')->help('请输入内容id');
        $form->action('/admin/filterobj/index/save');

        return $form->render();
    }

    public function editSubmit(Request $request, Content $content)
    {
        $request = $request->all();
        $rs = false;

        $data['obj_id'] = $request['obj_id'];
        $data['filter_type'] = FilterObj::EXPLORESHOP_TYPE;
        $data['filter_status'] = FilterObj::STATUS_NORMAL;
        $data['edittime'] = date('Y-m-d H:i:s', time());

        if(isset($request['id']) && $request['id']) {
            //修改
            $rs = FilterObj::updateById($request['id'], $data);
        } else {
            //添加
            $rs = FilterObj::add($data);
        }
        if($rs) {
            $success = new MessageBag([
                'title' => '提交成功'
            ]);
            return redirect('/admin/filterobj/index')->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '提交失败'
            ]);
            return redirect('/admin/filterobj/index')->with(compact('error'));
        }


    }


}